package com.yaoweigroup.community.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Base64;

public class Utils {

	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	public static final String DAY_NIGHT_MODE = "daynightmode";
	public static final String DEVIATION = "deviationrecalculation";
	public static final String JAM = "jamrecalculation";
	public static final String TRAFFIC = "trafficbroadcast";
	public static final String CAMERA = "camerabroadcast";
	public static final String SCREEN = "screenon";
	public static final String THEME = "theme";
	public static final String ISEMULATOR = "isemulator";

	public static final String ACTIVITYINDEX = "activityindex";

	public static final int SIMPLEHUDNAVIE = 0;
	public static final int EMULATORNAVI = 1;
	public static final int SIMPLEGPSNAVI = 2;
	public static final int SIMPLEROUTENAVI = 3;

	public static final boolean DAY_MODE = false;
	public static final boolean NIGHT_MODE = true;
	public static final boolean YES_MODE = true;
	public static final boolean NO_MODE = false;
	public static final boolean OPEN_MODE = true;
	public static final boolean CLOSE_MODE = false;

	public static String bitmaptoString(Bitmap bitmap) {

		// 将Bitmap转换成字符串

		String string = null;
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 100, bStream);
		byte[] bytes = bStream.toByteArray();
		// string = Base64.encodeBase64String(bytes);
		string = Base64.encodeToString(bytes, Base64.DEFAULT);
		return string;

	}

	public static boolean checkAPP(Context context, String packageName) {
		if (packageName == null || "".equals(packageName))
			return false;
		try {
			ApplicationInfo info = context.getPackageManager()
					.getApplicationInfo(packageName,
							PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

	public static Bitmap compressBmpFromBmp(Bitmap image) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int options = 100;
		image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		while (baos.toByteArray().length / 1024 > 100) {
			baos.reset();
			options -= 10;
			image.compress(Bitmap.CompressFormat.JPEG, options, baos);
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
		return bitmap;
	}

}
